import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layouts'
import EmptyLayout from '@/layouts/EmptyLayout'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true,
  },
  {
    path: '/401',
    name: '401',
    component: () => import('@/views/401'),
    hidden: true,
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    hidden: true,
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index/index'),
        meta: {
          title: '首页',
          icon: 'Home',
          affix: true,
        },
      },
    ],
  },
  // {
  //   path: '/analysisManagement',
  //   component: Layout,
  //   redirect: 'noRedirect',
  //   name: 'AnalysisManagement',
  //   title: '数据分析',
  //   alwaysShow: true,
  //   meta: { title: '数据分析', icon: 'Tongji' },
  //   children: [
  //     {
  //       path: 'flowingManagement',
  //       name: 'FlowingManagement',
  //       title: '出入库日志',
  //       component: () =>
  //         import('@/views/analysisManagement/flowingManagement/index'),
  //       meta: { title: '出入库日志' },
  //     },
  //     {
  //       path: 'andetailManagement',
  //       name: 'AndetailManagement',
  //       title: '库存明细',
  //       component: () =>
  //         import('@/views/analysisManagement/andetailManagement/index'),
  //       meta: { title: '库存明细' },
  //     }
  //   ]
  // },
  // {
  //   path: '*',
  //   redirect: '/404',
  //   hidden: true,
  // },
]

export const asyncRoutes = setMenuData([
  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/index',
  //   children: [
  //     {
  //       path: 'index',
  //       name: 'Index',
  //       component: () => import('@/views/index/index'),
  //       meta: {
  //         title: '首页',
  //         icon: 'Home',
  //         affix: true,
  //       },
  //     },
  //   ],
  // },
  // {
  //   path: '/inwarehouseManagement',
  //   component: Layout,
  //   redirect: 'noRedirect',
  //   name: 'InwarehouseManagement',
  //   alwaysShow: true,
  //   title: '库内作业',
  //   meta: { title: '库内作业', icon: 'Kunei' },
  //   children: [
  //     {
  //       path: 'maintenanceManagement',
  //       name: 'MaintenanceManagement',
  //       title: '挂架管理',
  //       component: () =>
  //         import('@/views/inwarehouseManagement/maintenanceManagement/index'),
  //       meta: { title: '挂架管理' },
  //     },
  //     {
  //       path: '/inwarehouseManagement/Warehousing',
  //       name: 'Warehousing',
  //       title: '入库管理',
  //       alwaysShow: false,
  //       component: EmptyLayout,
  //       meta: {
  //         title: '入库管理'
  //       },
  //       children: [
  //         {
  //           path: '/inwarehouseManagement/Warehousing/page1',
  //           name: 'Warehousing-page1',
  //           title: '入库单',
  //           meta: { title: '入库单' },
  //           component: () => import('@/views/inwarehouseManagement/Warehousing/page1')
  //         },
  //         {
  //           path: '/inwarehouseManagement/Warehousing/page2',
  //           name: 'Warehousing-page2',
  //           title: '入库任务单',
  //           meta: { title: '入库任务单' },
  //           component: () => import('@/views/inwarehouseManagement/Warehousing/page2'),
  //         }
  //       ],
  //     },
  //     {
  //       path: '/inwarehouseManagement/exWarehouse',
  //       component: EmptyLayout,
  //       name: 'exWarehouse',
  //       alwaysShow: false,
  //       meta: {
  //         title: '出库管理',
  //         permissions: ['admin']
  //       },
  //       title: '出库管理',
  //       children: [
  //         {
  //           path: '/inwarehouseManagement/exWarehouse/page1',
  //           name: 'exWarehouse-page1',
  //           title: '出库单',
  //           meta: { title: '出库单' },
  //           component: () => import('@/views/inwarehouseManagement/exWarehouse/page1')
  //         },
  //         {
  //           path: '/inwarehouseManagement/exWarehouse/page2',
  //           name: 'exWarehouse-page2',
  //           title: '出库任务单',
  //           meta: { title: '出库任务单' },
  //           component: () => import('@/views/inwarehouseManagement/exWarehouse/page2'),
  //         }
  //       ],
  //     },
  //     {
  //       path: '/inwarehouseManagement/scrapManagement',
  //       name: 'ScrapManagement',
  //       title: '报损管理',
  //       alwaysShow: false,
  //       component: EmptyLayout,
  //       meta: {
  //         title: '报损管理',
  //         permissions: ['admin']
  //       },
  //       children: [
  //         {
  //           path: '/inwarehouseManagement/scrapManagement/page1',
  //           name: 'scrapManagement-page1',
  //           title: '报损单',
  //           meta: { title: '报损单' },
  //           component: () => import('@/views/inwarehouseManagement/scrapManagement/page1')
  //         },
  //         {
  //           path: '/inwarehouseManagement/scrapManagement/page2',
  //           name: 'scrapManagement-page2',
  //           title: '报损任务单',
  //           meta: { title: '报损任务单' },
  //           component: () => import('@/views/inwarehouseManagement/scrapManagement/page2'),
  //         }
  //       ],
  //     },
  //     {
  //       path: 'flippingManagement',
  //       name: 'FlippingManagement',
  //       title: '调拨管理',
  //       meta: { title: '调拨管理' },
  //       alwaysShow: false,
  //       component: EmptyLayout,
  //       children: [
  //         {
  //           path: '/inwarehouseManagement/flippingManagement/page1',
  //           name: 'flippingManagement-page1',
  //           title: '调拨单',
  //           meta: { title: '调拨单' },
  //           component: () => import('@/views/inwarehouseManagement/flippingManagement/page1')
  //         },
  //         {
  //           path: '/inwarehouseManagement/flippingManagement/page2',
  //           name: 'flippingManagement-page2',
  //           title: '调拨任务单',
  //           meta: { title: '调拨任务单' },
  //           component: () => import('@/views/inwarehouseManagement/flippingManagement/page2'),
  //         }
  //       ],
  //     },
  //     {
  //       path: 'inventoryManagement',
  //       name: 'InventoryManagement',
  //       title: '盘点管理',
  //       meta: { title: '盘点管理', permissions: ['admin'] },
  //       alwaysShow: false,
  //       component: EmptyLayout,
  //       children: [
  //         {
  //           path: '/inwarehouseManagement/inventoryManagement/page1',
  //           name: 'inventoryManagement-page1',
  //           title: '盘点单',
  //           meta: { title: '盘点单' },
  //           component: () => import('@/views/inwarehouseManagement/inventoryManagement/page1')
  //         },
  //         {
  //           path: '/inwarehouseManagement/inventoryManagement/page2',
  //           name: 'inventoryManagement-page2',
  //           title: '盘点任务单',
  //           meta: { title: '盘点任务单' },
  //           component: () => import('@/views/inwarehouseManagement/inventoryManagement/page2'),
  //         }
  //       ],
  //     },
  //     {
  //       path: 'validityManagement',
  //       name: 'ValidityManagement',
  //       title: '维修管理',
  //       meta: { title: '维修管理', permissions: ['admin'] },
  //       alwaysShow: false,
  //       component: EmptyLayout,
  //       children: [
  //         {
  //           path: '/inwarehouseManagement/validityManagement/page1',
  //           name: 'validityManagement-page1',
  //           title: '维修单',
  //           meta: { title: '维修单' },
  //           component: () => import('@/views/inwarehouseManagement/validityManagement/page1')
  //         },
  //         {
  //           path: '/inwarehouseManagement/validityManagement/page2',
  //           name: 'validityManagement-page2',
  //           title: '维修任务单',
  //           meta: { title: '维修任务单' },
  //           component: () => import('@/views/inwarehouseManagement/validityManagement/page2'),
  //         }
  //       ],
  //     },
  //   ]
  // },
  // {
  //   path: '/foundationlManagement',
  //   component: Layout,
  //   redirect: 'noRedirect',
  //   name: 'FoundationManagement',
  //   alwaysShow: true,
  //   title: '基础设置',
  //   meta: { title: '基础设置', icon: 'Jichuxx' },
  //   children: [
  //     {
  //       path: 'warehouseManagement',
  //       name: 'WarehouseManagement',
  //       title: '仓库设置',
  //       component: () =>
  //         import('@/views/foundationlManagement/warehouseManagement/index'),
  //       meta: { title: '仓库设置' },
  //     },
  //     {
  //       path: 'pylonsManagement',
  //       name: 'PylonsManagement',
  //       title: '挂架类型',
  //       component: () =>
  //         import('@/views/foundationlManagement/pylonsManagement/index'),
  //       meta: { title: '挂架类型' },
  //     },
  //     {
  //       path: 'locationManagement',
  //       name: 'LocationManagement',
  //       title: '货位设置',
  //       component: () =>
  //         import('@/views/foundationlManagement/locationManagement/index'),
  //       meta: { title: '货位设置' },
  //     },
  //     {
  //       path: 'trayManagement',
  //       name: 'TrayManagement',
  //       title: '托盘设置',
  //       component: () =>
  //         import('@/views/foundationlManagement/trayManagement/index'),
  //       meta: { title: '托盘设置' },
  //     },
  //   ]
  // },
  // {
  //   path: '/personnelManagement',
  //   component: Layout,
  //   redirect: 'noRedirect',
  //   name: 'PersonnelManagement',
  //   title: '系统管理',
  //   meta: { title: '系统管理', icon: 'Setting', permissions: ['admin'] },
  //   children: [
  //     {
  //       path: 'institutionManagement',
  //       name: 'InstitutionManagement',
  //       title: '机构管理',
  //       component: () =>
  //         import('@/views/personnelManagement/institutionManagement/index'),
  //       meta: { title: '机构管理' },
  //     },
  //     {
  //       path: 'resourceManagement',
  //       name: 'ResourceManagement',
  //       title: '用户管理',
  //       component: () =>
  //         import('@/views/personnelManagement/resourceManagement/index'),
  //       meta: { title: '用户管理' },
  //     },
  //     {
  //       path: 'jueseManagement',
  //       name: 'JueseManagement',
  //       title: '角色管理',
  //       component: () =>
  //         import('@/views/personnelManagement/jueseManagement/index'),
  //       meta: { title: '角色管理' },
  //     },
  //     {
  //       path: 'menuManagement',
  //       name: 'MenuManagement',
  //       component: () =>
  //         import('@/views/personnelManagement/menuManagement/index'),
  //       meta: { title: '菜单管理', badge: 'New' },
  //     },
  //   ],
  // },
], 1)

const router = new VueRouter({
  base: publicPath,
  mode: routerMode,
  scrollBehavior: () => ({
    y: 0,
  }),
  routes: constantRoutes,
})
export function resetRouter() {
  location.reload()
}

// 每个菜单加个level层级
function setMenuData(data, n) {
  for (var i = 0; i < data.length; i++) {
    var item = data[i]
    item.level = (item.children && item.children.length == 1) ? n - 1 : n;
    if (item.children && item.children.length > 0) {
      item.children = setMenuData(item.children, item.level + 1)
    }
  }
  return data;
}

export default router
